using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ControlLib
{
    public class IpTextBox : Control
    {
        IpNumericDigit digit1 = new IpNumericDigit();
        IpNumericDigit digit2 = new IpNumericDigit();
        IpNumericDigit digit3 = new IpNumericDigit();
        IpNumericDigit digit4 = new IpNumericDigit();
        protected override void OnCreateControl()
        {

            base.OnCreateControl();
            digit1.Left = 0;
            digit1.Use223AsMax = !IsSubnetMask;
            digit1.SelectNext += new IpNumericDigit.SelectNextDelegate(digit1_SelectNext);
            this.Controls.Add(digit1);
            digit2.Left = 32;
            digit2.Use223AsMax = false;
            digit2.SelectNext += new IpNumericDigit.SelectNextDelegate(digit2_SelectNext);
            digit2.SelectPrevious += new IpNumericDigit.SelectPreviousDelegate(digit2_SelectPrevious);
            this.Controls.Add(digit2);
            digit3.Left = 64;
            digit3.Use223AsMax = false;
            digit3.SelectNext += new IpNumericDigit.SelectNextDelegate(digit3_SelectNext);
            digit3.SelectPrevious += new IpNumericDigit.SelectPreviousDelegate(digit3_SelectPrevious);
            this.Controls.Add(digit3);
            digit4.Left = 96;
            digit4.Use223AsMax = false;
            digit4.SelectPrevious += new IpNumericDigit.SelectPreviousDelegate(digit4_SelectPrevious);
            this.Controls.Add(digit4);
        }

        void digit4_SelectPrevious()
        {
            digit3.Focus();
            if (digit3.Text.Length > 0)
            {
                digit3.Text = digit3.Text.Substring(0, digit3.Text.Length - 1);
            }
            digit3.Select(digit3.Text.Length, 1);
        }

        void digit3_SelectPrevious()
        {
            digit2.Focus();
            if (digit2.Text.Length > 0)
            {
                digit2.Text = digit2.Text.Substring(0, digit2.Text.Length - 1);
            }
            digit2.Select(digit2.Text.Length, 1);
        }

        void digit2_SelectPrevious()
        {
            digit1.Focus();
            if (digit1.Text.Length > 0)
            {
                digit1.Text = digit1.Text.Substring(0, digit1.Text.Length - 1);
            }
            digit1.Select(digit1.Text.Length, 1);
        }



        void digit3_SelectNext()
        {
            digit4.Focus();
            digit4.SelectAll();
        }

        void digit2_SelectNext()
        {
            digit3.Focus();
            digit3.SelectAll();
        }

        void digit1_SelectNext()
        {
            digit2.Focus();
            digit2.SelectAll();
        }

        public new string Text
        {
            get
            {
                if (digit1.Text.Trim() == "" || digit2.Text.Trim() == "" || digit3.Text.Trim() == "" || digit4.Text.Trim() == "")
                {
                    return "";
                }
                else
                {
                    return digit1.Text.Trim() + "." + digit2.Text.Trim() + "." + digit3.Text.Trim() + "." + digit4.Text.Trim();
                }
            }
            set
            {
                if (value == "")
                {
                    digit1.Text = ""; digit2.Text = ""; digit3.Text = ""; digit4.Text = "";
                }
                else
                {
                    string[] sDigits = value.Split(".".ToCharArray());
                    digit1.Text = sDigits[0].Trim();
                    digit2.Text = sDigits[1].Trim();
                    digit3.Text = sDigits[2].Trim();
                    digit4.Text = sDigits[3].Trim();
                }
            }
        }
        private bool _IsSubnetMask = false;

        public bool IsSubnetMask
        {
            get { return _IsSubnetMask; }
            set { _IsSubnetMask = value; }
        }

    }
}
